﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using BOMS.DBUtility;//请先添加引用

namespace BOMS.DAL
{
    /// <summary>
    /// 数据访问类S_BatSubLog。
    /// </summary>
    public class S_BatSubLog
    {
        public S_BatSubLog()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(long LogID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from S_BatSubLog");
            strSql.Append("where LogID=@LogID ");
            SqlParameter[] parameters = {
					new SqlParameter("@LogID", SqlDbType.BigInt)};
            parameters[0].Value = LogID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        public DataTable GetRealTimeBatteryData(int Generation, string EquipID, string BatteryGroup)
        {
            //sp_Realtime_BatteryData
            StringBuilder strSql = new StringBuilder();

            SqlParameter[] parameters = {					
                    new SqlParameter("@Generation", SqlDbType.TinyInt),
                    new SqlParameter("@EquipID", SqlDbType.NVarChar,30),
                    new SqlParameter("@BatGrp", SqlDbType.NVarChar,50),
                    new SqlParameter("@DataSrcFlag",SqlDbType.TinyInt)        
                                        };
            parameters[0].Value = Generation;
            parameters[1].Value = EquipID;
            parameters[2].Value = BatteryGroup;
            parameters[3].Value = 1;

            BOMS.Model.S_BatSubLog model = new BOMS.Model.S_BatSubLog();
            DataSet ds = DbHelperSQL.RunProcedure("dbo.sp_Realtime_BatteryData", parameters,"RealData");

            return ds.Tables["RealData"];
          
        }
        /// <summary>
        /// 得到一个最接近的对象实体（-1电池组的，否则是电池的 ）
        /// </summary>
        public object GetNearlyModel(DateTime dtm,byte Generation,string EquipID,string BatteryGrp,int BatteryID)
        {

            StringBuilder strSql = new StringBuilder();
           
            SqlParameter[] parameters = {
					new SqlParameter("@Logtime", SqlDbType.DateTime),
                    new SqlParameter("@Generation", SqlDbType.TinyInt),
                    new SqlParameter("@EquipID", SqlDbType.NVarChar,30),
                    new SqlParameter("@BatteryGrp", SqlDbType.NVarChar,50),
                    new SqlParameter("@BatteryID", SqlDbType.Int)                    
                                        };
            parameters[0].Value = dtm;
            parameters[1].Value = Generation;
            parameters[2].Value = EquipID;
            parameters[3].Value = BatteryGrp;
            parameters[4].Value = BatteryID;


           
            DataSet ds = DbHelperSQL.RunProcedure("dbo.sp_sub_FindBatteryOrGrpState", parameters, "S_BatSubLog");

            if (ds.Tables["S_BatSubLog1"].Rows.Count > 0)//todo
            {
                if (BatteryID == -1)
                {
                    BOMS.Model.S_BatgrpSubLog gmodel = new Model.S_BatgrpSubLog();
                    DAL.S_BatgrpSubLog gDAL = new S_BatgrpSubLog();
                    gDAL.AssignModel(ds.Tables["S_BatSubLog1"].Rows[0], gmodel);
                    return gmodel;

                }
                else
                {
                    BOMS.Model.S_BatSubLog model = new BOMS.Model.S_BatSubLog();
                    AssignModel(ds.Tables["S_BatSubLog1"].Rows[0], model);
                    return model;
                }
                
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public BOMS.Model.S_BatSubLog GetModel(long LogID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 LogID,Generation,EquipID,BatteryGroup,BatteryID,Vval,Rval,Tval,Status,LogTime from S_BatSubLog");
            strSql.Append("where LogID=@LogID ");
            SqlParameter[] parameters = {
					new SqlParameter("@LogID", SqlDbType.BigInt)};
            parameters[0].Value = LogID;

            BOMS.Model.S_BatSubLog model = new BOMS.Model.S_BatSubLog();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                AssignModel(ds.Tables[0].Rows[0], model);
                return model;
            }
            else
            {
                return null;
            }
        }
        public void AssignModel(DataRow dr, Model.S_BatSubLog model)
        {
            if (dr.Table.Columns.Contains("LogID") && dr["LogID"].ToString() != "")
            {
                model.LogID = long.Parse(dr["LogID"].ToString());
            }
            if (dr["Generation"].ToString() != "")
            {
                model.Generation = int.Parse(dr["Generation"].ToString());
            }
            model.EquipID = dr["EquipID"].ToString();
            model.BatteryGroup = dr["BatteryGroup"].ToString();
            if (dr.Table.Columns.Contains("BatteryID") && dr["BatteryID"].ToString() != "")
            {
                model.BatteryID = int.Parse(dr["BatteryID"].ToString());
            }
            if (dr.Table.Columns.Contains("Vval") && dr["Vval"].ToString() != "")
            {
                model.Vval = decimal.Parse(dr["Vval"].ToString());
            }
            if (dr.Table.Columns.Contains("Rval") && dr["Rval"].ToString() != "")
            {
                model.Rval = decimal.Parse(dr["Rval"].ToString());
            }
            if (dr.Table.Columns.Contains("Tval") && dr["Tval"].ToString() != "")
            {
                model.Tval = decimal.Parse(dr["Tval"].ToString());
            }
            if (dr.Table.Columns.Contains("Capacity") && dr["Capacity"].ToString() != "")
            {
                model.Capacity = decimal.Parse(dr["Capacity"].ToString());
            }
            if (dr.Table.Columns.Contains("Status"))
            {
                 model.Status = dr["Status"].ToString();
             }
            if (dr["LogTime"].ToString() != "")
            {
                model.LogTime = DateTime.Parse(dr["LogTime"].ToString());
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select LogID,Generation,EquipID,BatteryGroup,BatteryID,Vval,Rval,Tval,Status,LogTime ");
            strSql.Append(" FROM S_BatSubLog ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetListFromPage(string strWhere, string strOrderby, int pageSize, int curPage, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select LogID,Generation,EquipID,BatteryGroup,BatteryID,Vval,Rval,Tval,Status,LogTime ");
            strSql.Append("from S_BatSubLog ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }           
            return DALUti.GetPageData(strSql.ToString(), strOrderby, pageSize, curPage, out recordCount);
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" LogID,Generation,EquipID,BatteryGroup,BatteryID,Vval,Rval,Tval,Status,LogTime ");
            strSql.Append(" FROM S_BatSubLog ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "S_BatSubLog";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法
    }
}
